package com.wl.DataBase;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * 主程序入口
 * @author TaiZai
 * @data 2024/10/9 17:08
 */
public class Main {
    private static final Scanner sc = new Scanner(System.in);
    private static Connection connection;

    public static void main(String[] args) {
        // 建立数据库连接
        connection = MysqlServer.connect();
        showMenu("login");
        int choice;

        while (true) {
            System.out.print("请选择操作：");
            choice = sc.nextInt();
            goTo(choice);
        }
    }

    /**
     * goTo，方便进行跳转
     */
    public static void goTo(int choice) {
        switch (choice) {
            case 1 -> {
                System.out.println("*********欢迎登陆*********");
                login();
            }
            case 2 -> {
                System.out.println("*********欢迎注册*********");
                registered();
            }
            case 3 -> {
                System.out.println("*********忘记密码*********");
                forgetPassword();
            }
            case 0 -> {
                MysqlServer.closeConnection(connection); // 关闭数据库连接
                System.exit(0);
            }
            default -> {
                System.out.println("请重新输入正确的操作数：");
                showMenu("login");
            }
        }
    }

    /**
     * 菜单
     */
    public static void showMenu(String type) {
        if (type.equals("login")) {
            System.out.println("***************欢迎来到智慧教辅系统***************");
            System.out.println("***************1. 登陆");
            System.out.println("***************2. 注册");
            System.out.println("***************3. 忘记密码");
            System.out.println("***************0. 退出");
        } else if (type.equals("home")) {
            System.out.println("***************欢迎进入智慧教辅系统***************");
            System.out.println("***************a. 班级模块");
            System.out.println("***************b. 课程模块");
            System.out.println("***************c. 其他模块");
            System.out.println("***************d. 个人信息");
        }
    }

    /**
     * 登录
     */
    public static void login() {
        String usernameOrAccount, password;
        System.out.print("请输入账号或用户名：");
        usernameOrAccount = sc.next();
        System.out.print("请输入密码：");
        password = sc.next();

        // 查询语句
        String query = "select count(id) from users where (username = ? or account = ?) and password = ?";

        try (PreparedStatement preparedStatement  = connection.prepareStatement(query)) {
            // 设置查询参数
            preparedStatement.setString(1, usernameOrAccount);
            preparedStatement.setString(2, usernameOrAccount);
            preparedStatement.setString(3, password);

            // 执行查询
            ResultSet resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                int count = resultSet.getInt(1); // 获取第一列的值
                if (count > 0) {
                    System.out.println("登录成功！");
                    // 在这里可以进入主菜单或其他逻辑
                    showMenu("home");
                } else {
                    System.out.println("用户名或密码错误，请重新登录。");
                    goTo(1);
                }
            }
        } catch (SQLException e) {
            System.out.println("查询用户时发生错误: " + e.getMessage());
        }
    }

    /**
     * 注册
     */
    public static void registered() {
        String username, password, phone, email, account;
        // 用户名校验
        while (true) {
            System.out.print("请输入用户名：");
            username = sc.next();
            if (RegisterUtils.checkUsername(connection, username)) break;
        }

        // 密码校验
        while (true) {
            System.out.print("请输入密码：");
            password = sc.next();
            if (RegisterUtils.checkPassword(password)) break;
        }

        // 确认密码校验
        while (true) {
            System.out.print("请确认密码：");
            String confirmPassword = sc.next();
            if (confirmPassword.equals(password)) break;
            System.out.println("两次输入的密码不一致，请重新输入。");
        }

        // 手机号码校验
        while (true) {
            System.out.print("请输入手机号码：");
            phone = sc.next();
            if (RegisterUtils.checkPhone(phone)) break;
        }

        // 邮箱校验
        while (true) {
            System.out.print("请输入邮箱：");
            email = sc.next();
            if (RegisterUtils.checkEmail(email)) break;
        }

        // 生成随机的账号
        account = RegisterUtils.generateAccount(connection);

        // 插入数据
        String insert = "insert into users (username, password, phone, email, account) values (?,?,?,?,?)";

        try (PreparedStatement preparedStatement = connection.prepareStatement(insert)) {
            // 设置插入参数
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            preparedStatement.setString(3, phone);
            preparedStatement.setString(4, email);
            preparedStatement.setString(5, account);

            // 执行插入
            preparedStatement.executeUpdate();

            System.out.println("注册成功！您的账号为: " + account);
        } catch (SQLException e) {
            System.out.println("注册时发生错误: " + e.getMessage());
        }

    }

    /**
     * 忘记密码
     */
    public static void forgetPassword() {
        String usernameOrAccount, phone, email;
        while (true) {
            System.out.print("请输入您的账号或用户名：");
            usernameOrAccount = sc.next();
            System.out.print("请输入你的手机号：");
            phone = sc.next();
            System.out.print("请输入你的邮箱：");
            email = sc.next();

            String query = "select password from users where (username = ? or account = ?) and phone = ? and email = ?";
            try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
                preparedStatement.setString(1, usernameOrAccount);
                preparedStatement.setString(2, usernameOrAccount);
                preparedStatement.setString(3, phone);
                preparedStatement.setString(4, email);

                ResultSet resultSet = preparedStatement.executeQuery();
                if (resultSet.next()) {
                    String password = resultSet.getString("password");
                    System.out.println("您的原始密码为: " + password);
                    break;
                } else {
                    System.out.println("您输入的账号或信息不匹配，请重新输入。");
                }
            } catch (SQLException e) {
                System.out.println("查询用户时发生错误: " + e.getMessage());
            }
        }
    }
}
